package com.zhuss.core.processor;

import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.appconfig.TokenProcessor;
import com.zhuss.bean.bo.core.login.wechat.CreateWxcodeBo;
import com.zhuss.bean.bo.core.user.wechat.BindUserOpenIdBo;
import com.zhuss.bean.bo.core.user.wechat.UnbindUserOpenIdBo;
import com.zhuss.bean.vo.core.user.DetailUserVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.core.db.dao.ext.UserDao;
import com.zhuss.core.db.entity.User;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.IsEnum;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.AabException;
import com.zhuss.util.JsonUtil;

/**
 * 绑定用户openId processor
 * @Author: zhushanshuo
 * @Date: 2020/03/26 15:51
 */
@Component("userOpenIdBind")
public class UserOpenIdBind extends AbstractProcessor<Object, BindUserOpenIdBo> implements Processor<Object, BindUserOpenIdBo> {

    @Autowired
    private UserDao userDao;
    
    @Autowired
	@Qualifier("userRead")
	private UserRead userRead;
    
    @Autowired
    private UserCheck userCheck;
    
    @Autowired
	@Qualifier("wxcodeCreate")
	private Processor<Object, CreateWxcodeBo> wxcodeCreate;
    
    @Autowired
	@Qualifier("userOpenIdUnbind")
	private Processor<Object, UnbindUserOpenIdBo> userOpenIdUnbind;
    
    @Autowired
	private TokenProcessor tokenProcessor;
    
    /**
     * 校验
     */
    @Override
    public void check(BindUserOpenIdBo bo) throws AabException {
    	
    }
    
    /**
     * 操作方法
     * @throws AabException 
     */
    @Override
    public Object process(BindUserOpenIdBo param) throws AabException {
    	
    	Long userId = param.getUserId();
    	String openId = param.getOpenId();
    	
    	List<User> userList = userRead.listByOpenId(openId);
    	
    	for (User user : userList) {
    		UnbindUserOpenIdBo unbindBo = new UnbindUserOpenIdBo();
    		BeanUtils.copyProperties(param, unbindBo);
    		unbindBo.setOpenId(openId);
    		unbindBo.setUserId(user.getUserId());
    		userOpenIdUnbind.process(unbindBo);
    	}
    	
    	DetailUserVo userVo = (DetailUserVo) userRead.detail(userId);
    	
    	String oldOpenId = userVo.getOpenId();
    	
    	userList = userRead.listByOpenId(oldOpenId);
    	User userUnbind = null;
    	for (User user : userList) {
    		if (IsEnum.否.sameKey(user.getIsBind())) {
    			userUnbind = user;
    		}
    	}
    	
    	UnbindUserOpenIdBo unbindBo = new UnbindUserOpenIdBo();
    	unbindBo.setUserId(userId);
    	userOpenIdUnbind.process(unbindBo);
    	
    	if (userVo.getOpenId() != null && !"".equals(userVo.getOpenId())) {
    		tokenProcessor.delUserId(userVo.getOpenId(), userId);
    		tokenProcessor.setUserIdForever(userVo.getOpenId(), userUnbind.getUserId());
    	}
    	
    	User record = new User();
		record.setUserId(userId);
		record.setOpenId(openId);
		
		record.setModifierId(param.getConcurrentUserId());
    	record.setModifier(param.getConcurrentUserName());
    	record.setModifyTime(new Date());
		userDao.updateByPrimaryKeySelective(record);
		
		CreateWxcodeBo bo = new CreateWxcodeBo();
		BeanUtils.copyProperties(param, bo);
		bo.setUserId(userId);
		bo.setOpenId(openId);
		wxcodeCreate.process(bo);
    	
    	String name = ((DetailUserVo) userRead.detail(userId)).getName();
    	
    	param.setName(name);
        return null;
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(BindUserOpenIdBo param) throws AabException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.基础模块.key());
    	record.setAction(LogActionEnum.用户绑定openId.key());
    	record.setTargetId(param.getUserId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
